package com.cskaoyan.wxcontroller;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.coupon.CouponBO;
import com.cskaoyan.service.CouponService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description: 小程序ad、coupon、topic相关信息返回
 * @author: zhiMing
 * @date:
 */
@RestController
@RequestMapping("/wx")
public class WxCouponController {
    @Autowired
    CouponService couponService;

    @GetMapping("/coupon/mylist")
    public BaseRespVo<BaseData> myCouponList(Short status, BaseParam baseParam) {
        BaseData baseData = couponService.couponListWithOutSort(status, baseParam);
        return BaseRespVo.ok(baseData);
    }

    @PostMapping("/coupon/receive")
    public BaseRespVo receiveCoupon(@RequestBody CouponBO couponBO) {
        //TODO 这里应该也需要用户session里的id，把优惠券保存到指定用户上
        // note：要把coupon表里的优惠券根据couponId减一，并且在coupon_user里增加一条记录，
        //coupon_user里的日期要根据coupon里的日期或者day进行计算
        // 前台查的话，也要查询有效时间范围内的优惠券，并且优惠券库存没了也不能显示
        int code = couponService.couponReceive(couponBO.getCouponId());
        if (code == -1)
            return BaseRespVo.error(402, "领取次数有限");
        else if (code == 501)
            return BaseRespVo.error(501, "未登录");
        return BaseRespVo.ok(null);
    }

    @PostMapping("/coupon/exchange")
    public BaseRespVo exchangeCoupon(@RequestBody CouponBO couponBO) {
        //根据兑换码去查找，获取兑换码所对应的优惠券信息
        int code = couponService.couponExchange(couponBO.getCode());
        if (code == -1)
            return BaseRespVo.error(402, "领取次数有限");
        else if (code == -2)
            return BaseRespVo.error(402, "兑换码不存在");
        else if (code == -3)
            return BaseRespVo.error(402, "已经领完了");
        else if (code == -4)
            return BaseRespVo.error(402, "该券过期了");
        return BaseRespVo.ok(null);
    }

    @GetMapping("/coupon/selectlist")
    public BaseRespVo<BaseData> selectList(Integer cartId, Integer grouponRulesId) {
        BaseData baseData = couponService.couponListWithOutSort();
        return BaseRespVo.ok(baseData);
    }
}
